package com.masterclient.app.modules.pay.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselibrary.bean.pay.AliPayInfoBean;
import com.eoner.baselibrary.bean.pay.PaymentInfoBean;
import com.eoner.baselibrary.bean.pay.WechatPayInfoBean;
import com.masterclient.app.httpservice.RetrofitUtil2;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;


public class PayPresenter extends PayContract.Presenter{



    @Override
    public void get_payment_info(int pay_amount, String channel, String pay_order_no, String uid) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("pay_amount",pay_amount);
            jsonObject.put("channel",channel);
            jsonObject.put("pay_order_no",pay_order_no);
            jsonObject.put("uid",uid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<PaymentInfoBean> observable = RetrofitUtil2.getInstance().get_payment_info(body);
        Disposable disposable = toSubscribe(observable, new Consumer<PaymentInfoBean>() {
            @Override
            public void accept(@NonNull PaymentInfoBean bean) throws Exception {
                if(bean != null){
                    mView.get_payment_info_success(bean);
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);

    }

    @Override
    public void get_wx_prepay(String channel, String order_no) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("channel",channel);
            jsonObject.put("order_no",order_no);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<WechatPayInfoBean> observable = RetrofitUtil2.getInstance().get_wx_prepay(body);
        Disposable disposable = toSubscribe(observable, new Consumer<WechatPayInfoBean>() {
            @Override
            public void accept(@NonNull WechatPayInfoBean bean) throws Exception {
                if(bean != null){
                    mView.get_wx_prepay_success(bean);
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);

    }

    @Override
    public void get_alipay_prepay(String channel, String order_no) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("channel",channel);
            jsonObject.put("order_no",order_no);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<AliPayInfoBean> observable = RetrofitUtil2.getInstance().get_alipay_prepay(body);
        Disposable disposable = toSubscribe(observable, new Consumer<AliPayInfoBean>() {
            @Override
            public void accept(@NonNull AliPayInfoBean bean) throws Exception {
                if(bean != null){
                    mView.get_alipay_prepay_uccess(bean);
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }
}